{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import sympy as S\n",
    "from sympy import stats\n",
    "p = S.symbols('p',positive=True,real=True)\n",
    "x=stats.Binomial('x',5,p)\n",
    "t = S.symbols('t',real=True,positive=True)\n",
    "mgf = stats.E(S.exp(t*x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from IPython.display import Math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$p^{5} e^{5 t} + 5 p^{4} \\left(- p + 1\\right) e^{4 t} + 10 p^{3} \\left(p - 1\\right)^{2} e^{3 t} - 10 p^{2} \\left(p - 1\\right)^{3} e^{2 t} + 5 p \\left(p - 1\\right)^{4} e^{t} - \\left(p - 1\\right)^{5}$$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(S.latex(S.simplify(mgf)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import scipy.stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def gen_sample(ns=100,n=10):\n",
    "    out =[]\n",
    "    for i in range(ns):\n",
    "        p=scipy.stats.uniform(0,1).rvs()\n",
    "        out.append(scipy.stats.binom(n,p).rvs())\n",
    "    return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({3: 104, 9: 103, 0: 95, 10: 95, 2: 93, 4: 90, 7: 90, 8: 90, 5: 87, 6: 83, 1: 70})"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEACAYAAABI5zaHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAD5RJREFUeJzt3W2sZVV9x/HvzxmfeNCRmAzDMAm0OhWIGjFaUmsccbQT\nYgbeFDXVjkpfoUh4QTqjSTuvCGqsmja88AEybYUWkRBISDpX5BqbGpCAiAx0pHGUgczFWjW11MiE\nf1+cbTy9Dnfu2edp7uL7SQh7rbP3Xv8z9+Z311ln73NSVUiS2vKCeRcgSZo8w12SGmS4S1KDDHdJ\napDhLkkNMtwlqUErhnuS65MsJXloqO/TSR5J8mCSW5O8fOixPUl+kOTRJO+aZuGSpOd2vJn7DcCO\nZX37gfOq6vXAQWAPQJJzgfcA53bHXJfEVwaSNAcrhm9VfQv42bK+hap6tmveA5zZbV8M3FRVz1TV\nIeAx4M2TLVeStBrjzqw/DNzZbZ8BHB567DCweczzS5J66B3uST4B/LqqblxhNz/bQJLmYH2fg5J8\nELgIeMdQ9xPAlqH2mV3f8mMNfEnqoaqy2n1HDvckO4CrgbdV1a+GHroduDHJ3zBYjnk1cO+4Ba41\nSfZW1d551zEtPr+1reXn1/Jzg9EnxiuGe5KbgLcBr0zyOPDXDK6OeRGwkATg21V1eVUdSHIzcAA4\nClxefuSkJM3FiuFeVe87Rvf1K+x/DXDNuEVJksbjdeiTtzjvAqZscd4FTNnivAuYssV5FzBFi/Mu\n4ESSWa+cJKmW19wlaRpGzU5n7pLUIMNdkhpkuEtSgwx3SWqQ4S5JDTLcJalBhrskNchwl6QGGe6S\n1CDDXZIaZLhLUoMMd0lqk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b0aSFwJfA/6xqm6bdz0T9EfAziQ/BG4CLkzy93OuaZIOA4er\n6jtd+xYGYd+K7cAPq+qnVXUUuJXBz7Q1S0lOB0iyCXhqzvVMXJIPMlgePe4f51mG+33Aq5OcleRF\nwHuA22c4/lQlCfBl4EBVfW7e9UxSVX28qrZU1dkM3oj7RlX9+bzrmpSqOgI8nmRr17UdeHiOJU3a\nj4ALkry0+z3dzuCN8dbcDuzqtncBLU2wSLKDwdLoxVX1q+PtP7Nw72YMv7nB6QDwz43d4PQW4P3A\n27vLBR/ofhgtavHl7hXAV5I8yOBqmWvmXM/EVNW9DF6N3A98r+v+wvwqGl+Sm4B/A/4gyeNJPgRc\nC7wzyUHgwq69Jh3j+X0Y+FvgFGChy5frVjyHNzFJUnv8mj1JapDhLkkNMtwlqUGGuyQ1yHCXpAYZ\n7pLUIMNdkhpkuEtSg/4PWTNUyTHh/T8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x8f1a390>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "xs = gen_sample(1000)\n",
    "hist(xs,range=(1,10),align='mid')\n",
    "Counter(xs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## sum of IID normally distributed random variables "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "S.var('x:2',real=True)\n",
    "S.var('mu:2',real=True)\n",
    "S.var('sigma:2',positive=True)\n",
    "S.var('t',positive=True)\n",
    "x0=stats.Normal(x0,mu0,sigma0)\n",
    "x1=stats.Normal(x1,mu1,sigma1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "mgf0=S.simplify(stats.E(S.exp(t*x0)))\n",
    "mgf1=S.simplify(stats.E(S.exp(t*x1)))\n",
    "mgfY=S.simplify(mgf0*mgf1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$e^{\\mu_{0} t + \\frac{\\sigma_{0}^{2} t^{2}}{2}}$$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(S.latex(mgf0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$e^{\\frac{t}{2} \\left(2 \\mu_{0} + 2 \\mu_{1} + \\sigma_{0}^{2} t + \\sigma_{1}^{2} t\\right)}$$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(S.latex(S.powsimp(mgfY)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "t**2*(sigma0**2/2 + sigma1**2/2) + t*(mu0 + mu1)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S.collect(S.expand(S.log(mgfY)),t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
